package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        int currentSize = bookList.getUsedSize();
        System.out.println("删除图书");
        //若书架为空
        if(currentSize == 0){
            System.out.println("书架为空");
            return;
        }
        System.out.println("请输入你要删除的图书名称");
        Scanner in = new Scanner(System.in);
        String name = in.nextLine();
        //若书架为空
        if(currentSize == 0){
            System.out.println("书架为空");
            return;
        }
        int index = -1;
        //查找是否有这本书
        for(int i =0; i < currentSize; i++){
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)){
                index = i;
                break;
            }
        }
        //若没有这本书
        if(index == -1){
            System.out.println("没有这本书");
            return;
        }
        //查找到，从index开始删除
        for(int i = index; i < currentSize - 1; i++){
            Book book = bookList.getBooks(i+1);
            bookList.setBooks(i, book);
        }
        System.out.println("删除成功");
        bookList.setUsedSize(currentSize - 1);
    }
}
